package com.lxl.controller.department;

import com.lxl.mapper.DepartmentMapper;
import com.lxl.pojo.Department;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 开启swagger-ui
 * @Date 2021/11/26 16:14
 * @Created by LXLDEMO
 */
@Api(tags = "department 的crud")
@RequestMapping("/department4swagger")
@RestController
public class DepartmentController4Swagger {
    @Autowired
    private DepartmentMapper departmentMapper;
    /**
     * 测试接口
     * ApiOperation 接口描述
     * GetMappin 请求方式：get  请求路径：""
     * GetMappin:：
     *      不写值，默认是"",即使用http:localhost:8082/test即可访问，
     *      注意同一个controller中不能有两个相同的路径
     * @return
     */
    @ApiOperation("测试接口")
    @GetMapping
    public String success(){
        return "test succeed.";
    }

    @ApiOperation("获取所有department信息")
    @GetMapping("/getAll")
    public List<Department> getAll(){
        return departmentMapper.getAll();
    }

    /**
     * 根据id获取 department信息
     * @param id
     * @return
     */
    @ApiOperation("根据id获取department信息")
    @PostMapping("/getById")
    @ResponseBody
    public String getDepartmentById(@RequestBody Integer id){
        Map<String,Object> param = new HashMap<>();
        param.put("id","" + id);
        return "" + departmentMapper.getDepartmentById(param);
    }

    /**
     * 根据id删除 department信息
     * @param id
     * @return
     */
    @ApiOperation("根据id删除department信息")
    @DeleteMapping("/delById")
    @ResponseBody
    public String delById(@RequestBody Integer id){
        Map<String,Object> param = new HashMap<>();
        param.put("id","" + id);
        Integer result = departmentMapper.delDepartmentById(param);
        return result == 0?"删除失败":"删除成功";
    }

    /**
     * 增加 department信息
     * @param department
     * @return
     */
    @ApiOperation("新增 not map")
    @PostMapping("/addDepartment")
    @ResponseBody
    public String addDepartment(@RequestBody Department department){
        Map<String,Object> param = new HashMap<>();
        param.put("id",department.getId());
        param.put("departmentName",department.getDepartmentName());
        System.out.println(department);
        Integer result = departmentMapper.addDepartment(param);
        return result == 0?"插入失败":"插入成功";
    }


    /**
     * 更新 department信息
     * @param department
     * @return
     */
    @ApiOperation("新增")
    @PostMapping("/updateDepartmentById")
    @ResponseBody
    public String updateDepartmentById(@RequestBody Department department){
        System.out.println(department);
        Map<String,Object> param = new HashMap<>();
        param.put("id",department.getId());
        param.put("departmentName",department.getDepartmentName());
        System.out.println(department);
        Integer result = departmentMapper.updateDepartmentById(param);
        return result == 0?"更新失败":"更新成功";
    }






}
